package net.lab1024.smartadmin.module.business.engine.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.smartadmin.common.domain.PageResultDTO;
import net.lab1024.smartadmin.common.domain.ResponseDTO;
import net.lab1024.smartadmin.module.business.engine.dao.EngineDao;
import net.lab1024.smartadmin.module.business.engine.domain.dto.EngineAddDTO;
import net.lab1024.smartadmin.module.business.engine.domain.dto.EngineQueryDTO;
import net.lab1024.smartadmin.module.business.engine.domain.dto.EngineUpdateDTO;
import net.lab1024.smartadmin.module.business.engine.domain.entity.EngineEntity;
import net.lab1024.smartadmin.module.business.engine.domain.vo.EngineExcelVO;
import net.lab1024.smartadmin.module.business.engine.domain.vo.EngineVO;
import net.lab1024.smartadmin.util.SmartBeanUtil;
import net.lab1024.smartadmin.util.SmartPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * [ ������ ]
 *
 * @author �Ζ�
 * @version 1.0
 * @company ��������
 * @copyright (c)  ��������Inc. All rights reserved.
 * @date 2021-11-19 11:18:28
 * @since JDK1.8
 */
@Service
public class EngineService {

    @Autowired
    private EngineDao engineDao;

    /**
     * ���ݱ�Ų�ѯ
     */
    public Boolean select(String sn,Long id){
        QueryWrapper<EngineEntity> queryWrapper= new QueryWrapper<>();
        queryWrapper.lambda().like(EngineEntity::getSn,sn);
        if(id!=null){
            queryWrapper.lambda().ne(EngineEntity::getId, id);
        }
        return  engineDao.selectList(queryWrapper).size()>0;
    }

    /**
     * ����id��ѯ
     */
    public EngineEntity getById(Long id){
        return  engineDao.selectById(id);
    }

    /**
     * ��ҳ��ѯ
     * @author �Ζ�
     * @date 2021-11-19 11:18:28
     */
    public ResponseDTO<PageResultDTO<EngineVO>> queryByPage(EngineQueryDTO queryDTO) {
        Page page = SmartPageUtil.convert2QueryPage(queryDTO);
        IPage<EngineVO> voList = engineDao.queryByPage(page, queryDTO);
        PageResultDTO<EngineVO> pageResultDTO = SmartPageUtil.convert2PageResult(voList);
        return ResponseDTO.succData(pageResultDTO);
    }

    /**
     * ���
     * @author �Ζ�
     * @date 2021-11-19 11:18:28
     */
    public ResponseDTO<String> add(EngineAddDTO addDTO) {
        EngineEntity entity = SmartBeanUtil.copy(addDTO, EngineEntity.class);
        engineDao.insert(entity);
        return ResponseDTO.succ();
    }

    /**
     * �༭
     * @author �Ζ�
     * @date 2021-11-19 11:18:28
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(EngineUpdateDTO updateDTO) {
        EngineEntity entity = SmartBeanUtil.copy(updateDTO, EngineEntity.class);
        engineDao.updateById(entity);
        return ResponseDTO.succ();
    }

    /**
     * ɾ��
     * @author �Ζ�
     * @date 2021-11-19 11:18:28
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> deleteByIds(List<Long> idList) {
        engineDao.deleteByIdList(idList);
        return ResponseDTO.succ();
    }

    /**
     * ��ѯȫ����������
     * @author �Ζ�
     * @date 2021-11-19 11:18:28
     */
    public List<EngineExcelVO> queryAllExportData(EngineQueryDTO queryDTO) {
        return engineDao.queryAllExportData( queryDTO);
    }

    /**
     * ������ѯ��������
     * @author �Ζ�
     * @date 2021-11-19 11:18:28
     */
    public List<EngineExcelVO> queryBatchExportData(List<Long> idList) {
        return engineDao.queryBatchExportData(idList);
    }
}
